package jym.tan.movielibrary.component.ui.panel;

import java.awt.Color;
import java.awt.Image;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import jym.tan.movielibrary.constant.LayoutConstant;
import jym.tan.movielibrary.factory.ComponentFactory;
import jym.tan.movielibrary.factory.ImageFactory;
import jym.tan.movielibrary.factory.LayoutFactory;
import jym.tan.movielibrary.logger.LibraryLogger;
import jym.tan.movielibrary.model.MovieSearchModel;

import org.apache.commons.lang.StringUtils;

public class MovieSearchInfoPanel extends GradientBackgroundPanel<MovieSearchInfoPanel> {

	private static final long serialVersionUID = 56L;
	
	public MovieSearchInfoPanel(Color top, Color bottom) {
		super(top, bottom);
	}

	@Override
	public void initialize() {
		setLayout(LayoutFactory.getMigDefaultLayout());
		add(ComponentFactory.getMovieSearchNameLabel(), LayoutConstant.WRAP);
		add(ComponentFactory.getMovieSearchYearLabel(), LayoutConstant.WRAP);
		add(ComponentFactory.getMovieSearchPosterLabel(), LayoutConstant.WRAP);
		add(ComponentFactory.getImdbLogoLabel());		
	}	

	public void update(MovieSearchModel movieSearchModel) {
		if(movieSearchModel == null) {
			ComponentFactory.getMovieSearchNameLabel().setText("");
			ComponentFactory.getMovieSearchPosterLabel().setIcon(null);
			changeChildComponentVisibility(false);
			return;
		}
		Image image = null;
		try {
			image = ImageIO.read(new URL(movieSearchModel.getPosters()[0].getImage().getUrl()).openStream());
		} catch (Exception exception) {
			LibraryLogger.errorMessage(exception.getMessage());
		}
		
		if(image != null) {
			ComponentFactory.getMovieSearchPosterLabel().setIcon(new ImageIcon(image));
			ComponentFactory.getMovieSearchPosterLabel().setVisible(true);
		} else {
			ComponentFactory.getMovieSearchPosterLabel().setVisible(false);
		}
		
		if(StringUtils.isNotBlank(movieSearchModel.getName())) {
			ComponentFactory.getMovieSearchNameLabel().setText(movieSearchModel.getName());
			ComponentFactory.getMovieSearchNameLabel().setVisible(true);
		} else {
			ComponentFactory.getMovieSearchNameLabel().setVisible(false);
		}
		
		if(movieSearchModel.getReleased() != null) {
			Date released = movieSearchModel.getReleased();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(released);
			String year = "(" + Integer.valueOf(calendar.get(Calendar.YEAR)).toString() + ")";
			ComponentFactory.getMovieSearchYearLabel().setText(year);
			ComponentFactory.getMovieSearchYearLabel().setVisible(true);
		} else {
			ComponentFactory.getMovieSearchYearLabel().setVisible(false);
		}
		
		Image imdbLogo = null;
		
		try {
			imdbLogo = ImageFactory.getIMDbLogo();
		} catch (Exception exception) {
			LibraryLogger.errorMessage(exception.getMessage());
		}
		
		if(imdbLogo != null) {
			ComponentFactory.getImdbLogoLabel().setIcon(new ImageIcon(imdbLogo));
			ComponentFactory.getImdbLogoLabel().setVisible(true);
		} else {
			ComponentFactory.getImdbLogoLabel().setVisible(false);
		}
	}
	
	public void changeChildComponentVisibility(boolean visible) {
		ComponentFactory.getMovieSearchPosterLabel().setVisible(visible);
		ComponentFactory.getMovieSearchNameLabel().setVisible(visible);
		ComponentFactory.getMovieSearchYearLabel().setVisible(visible);
		ComponentFactory.getImdbLogoLabel().setVisible(visible);
	}
	
	/*
	 * JLabel jLabel = new JLabel();		
		jLabel.setText("<html>"
							+"<body>\n"+
								"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"
									+ "<tr>\n" + "<td style=\"font-weight: bold;\">\n" + "cell1\n" + "</td>\n" + "<td style=\"padding-left: 5px\">\n" + ":\n" + "</td>\n" + "<td style=\"padding-left: 20px; font-style: italic;\">\n" + "cell2\n" + "</td>\n" + "</tr>\n"
									+ "<tr>\n" + "<td style=\"font-weight: bold;\">\n" + "cell3 cell5\n" + "</td>\n" + "<td style=\"padding-left: 5px\">\n" + ":\n" + "</td>\n" +"<td class=\"leftcell\" style=\"padding-left: 20px; font-style: italic;\">\n" + "cell4\n" + "</td>\n" + "</tr>\n"								
								+ "</table>"
							+ "</body>"
						+"</html>");		
		add(jLabel);
		jLabel.setForeground(Color.white);
	 * 
	 * */
}
